﻿using System;
using System.Threading;

namespace SFJDService.SocketHandle
{
    class Synchronization
    {
        public bool isWorking = false;

        public Synchronization(SynType synType)
        {
            isWorking = true;
            new Thread(() =>
            {
                try
                {
                    switch (synType)
                    {
                        case SynType.Brigade:
                            SynBrigade();
                            break;
                        case SynType.Contact:
                            SynContact();
                            break;
                        case SynType.User:
                            SynUser();
                            break;
                    }
                }
                catch(Exception e)
                {
                    Runtime.Printf(GetType().Name + " :" + e.Message);
                }
                finally
                {
                    isWorking = false;
                }
            }).Start();
        }

        /// <summary>
        /// 同步Brigade.xml数据
        /// </summary>
        private void SynBrigade()
        {
            Launcher.SendMsg2All(typeof(PH02GetFileSize).GetDictianaryKey(), "0");
            Thread.Sleep(2000);
            long max = -1;
            ClassActive buf = new ClassActive();
            bool isThereLess = false;
            try
            {
                foreach (ClassActive active in Runtime.UsersOnLine.activeUsers)
                {
                    if (active.BrigadeSize > max)
                    {
                        max = active.BrigadeSize;
                        buf = active;
                    }
                }
                foreach (ClassActive active in Runtime.UsersOnLine.activeUsers)
                {
                    if (active.BrigadeSize != 0 & active.BrigadeSize < max)
                    {
                        isThereLess = true;
                        break;
                    }
                }
                //广播头等奖中奖人的信息，其余各位参赛者根据自己的参赛信息自行决定是否收听广播，中奖人听到广播后开始广播自己的全部资源
                if (isThereLess)
                {
                    Launcher.SendMsg2All(typeof(PH02PublishWinner).GetDictianaryKey(), buf.Ip);
                }
            }
            catch { }
        }

        /// <summary>
        /// 同步Contact.xml数据
        /// </summary>
        private void SynContact()
        {
            Launcher.SendMsg2All(typeof(PH03GetFileSize).GetDictianaryKey(), "0");
            Thread.Sleep(2000);
            long max = -1;
            ClassActive buf = new ClassActive();
            bool isThereLess = false;
            try
            {
                foreach (ClassActive active in Runtime.UsersOnLine.activeUsers)
                {
                    if (active.ContactSize > max)
                    {
                        max = active.ContactSize;
                        buf = active;
                    }
                }
                foreach (ClassActive active in Runtime.UsersOnLine.activeUsers)
                {
                    if (active.ContactSize != 0 & active.ContactSize < max)
                    {
                        isThereLess = true;
                        break;
                    }
                }
                //广播头等奖中奖人的信息，其余各位参赛者根据自己的参赛信息自行决定是否收听广播，中奖人听到广播后开始广播自己的全部资源
                if (isThereLess)
                {
                    Launcher.SendMsg2All(typeof(PH03PublishWinner).GetDictianaryKey(), buf.Ip);
                }
            }
            catch { }
        }

        /// <summary>
        /// 同步UsersData.xml数据
        /// </summary>
        private void SynUser()
        {
            ;
        }
    }
    enum SynType
    {
        Brigade, Contact, User
    }
}
